package com.itheima.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        String userName = request.getParameter("name");
        String password = request.getParameter("password");
        //检测用户名和密码
        if(userName.equals("Jack")&&password.equals("123")){
            //核对正确,向域中存放数据
            ServletContext context = super.getServletContext();
            context.setAttribute("username",userName);
            //跳转
            request.getRequestDispatcher("/sucessServlet").forward(request,response);
        }else {
//            resp.sendRedirect("/failure");
            request.getRequestDispatcher("/failure.html").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
